********************************************************************
*     		 	    HOSPITAL-LEVEL SERVER CODE  	        	   *
* Author: Maggie Shi 	 									       *
* 														           *
* Purpose: This code builds the dataset for the hospital-level     *
* analysis in Shi (2023). 										   *
* 														           *
* Last edited: 8/14/2023 									       *
* 														           *
* Notes: Running this code requires access to the 100% Medicare    *
*	Inpatient file, 100% Medicare Output files, and 100% Medicare  *
*	MEDPAR Research Identifiable files, and associated Substance   *
*	Use Disorder files. It also requires the Master Beneficiary    *
*   files. 														   *
* 	The data from "~Local-data/CMS-server-data" in the 	    	   *
* 	replication packet should be copied into the 			  	   *
* 	"${DataPath}" path below. 							       	   *
* 	See https://resdac.org/research-identifiable-files-rif-requests*
*  	for information on accessing the CMS RIF files. 			   *
********************************************************************
cap log close

*--------------------------------------------------
* Set up paths -- change these to point to where the raw data is
*--------------------------------------------------
clear all
global CodePath        "/homes/nber/m.shi-dua55451/sacarny-DUA55451/m.shi-dua55451/Code/CMS-server-code" 		// the do-files should be in this folder
global DataPath        "/homes/nber/m.shi-dua55451/sacarny-DUA55451/m.shi-dua55451/Data/CMS-server-data"
global McareDataPath   "/disk/aging/medicare/data" 													    		// raw Medicare data path
global SUDDataPath     "/disk/aging/sud/data"																    // raw SUD data path
global OutputPath      "/homes/nber/m.shi-dua55451/sacarny-DUA55451/m.shi-dua55451/Output"		

cap mkdir 			   "${OutputPath}/"
cap mkdir      	       "${DataPath}/"
cap mkdir              "${DataPath}/claims/"
cap mkdir              "${DataPath}/emergencyvisits/"
cap mkdir 		       "${DataPath}/observation/"
cap mkdir 		       "${DataPath}/derived"
cap mkdir              "${CodePath}/log"
cap mkdir 			   "${DataPath}/within150m/"
cap mkdir 			   "${DataPath}/within100m/"
cap mkdir 			   "${DataPath}/within50m/"

set maxvar 10000
set matsize 5000
*--------------------------------------------------
* Load required packages
*--------------------------------------------------
cap ssc install gtools
cap ssc install rangestat
cap ssc install texdoc 
cap ssc install estout, replace
cap ssc install estwrite
cap ssc install mipolate
cap ssc install regsave
cap ssc install elabel
cap ssc install binscatterhist
cap ssc install addplot

*--------------------------------------------------
* Define programs
*--------------------------------------------------
	do "${CodePath}/repl_defineprograms.do"

*--------------------------------------------------
* Build data
*--------------------------------------------------
	// merge audit data to inpatient claims data
	do "${CodePath}/repl_mergeaudits.do"

	// summarize MEDPAR
	do "${CodePath}/repl_medparsummary.do"

	// identify emergency visits 
	do "${CodePath}/repl_identifyemergency.do"	

	// identify observation stays
	do "${CodePath}/repl_identify_obs.do"	

	// identify long outpatient stays 
	do "${CodePath}/repl_identify_longoutpatient.do"	

	// identify long outpatient stays 
	do "${CodePath}/repl_identify_obs_combine.do"	

	// build hospital-year dataset
	do "${CodePath}/repl_buildhospitalyear.do"	

	// create jackknife audit rates and add in GIS elements to hospital-year dataset
	do "${CodePath}/repl_jackknife_build.do"	 

	// create hospital comparison groups
	do "${CodePath}/repl_clean_comparisongroups.do"

	// build hospital closure data
	do "${CodePath}/repl_build_hospclosure.do"

	// build 2010 file for comparison to HCUP data
	do "${CodePath}/repl_build_medpar_summarystats_2010.do"

*--------------------------------------------------
* Summary Statistics
*--------------------------------------------------
	// summarize across RACs and time
	do "${CodePath}/repl_summarize_auditstrategies.do"

	// summary statistics on main sample
	do "${CodePath}/repl_summarystats.do"

	// summary stats on medpar to compare to HCUP
	do "${CodePath}/repl_medpar_summarystats_2010.do"	

	// compare within hospital comparison group and overall
	do "${CodePath}/repl_compare_hospcompgroup_overall.do"

*--------------------------------------------------
* Analysis
*--------------------------------------------------
	// main analysis: 100-mile hospital comparison groups
	do "${CodePath}/repl_regressions_100m.do"

	// robustness: 50-mile hospital comparison groups
	do "${CodePath}/repl_regressions_50m.do"

	// robustness: 150-mile hospital comparison groups
	do "${CodePath}/repl_regressions_150m.do"

	// all hospitals
	do "${CodePath}/repl_regressions_all.do"

	// robustness: run regressions comparing hospitals to neighbors, but excluding individual states or hospital comparison groups
	do "${CodePath}/repl_regs_hosp_100mradius_bystate_nbrgroup.do"

	// robustness: run regressions excluding hospitals closest to the border and where neighbor is defined as hospitals outside of the same HRR or chain
	do "${CodePath}/repl_regs_hosp_100mradius_donut.do"

	// falsification test: internal borders
	do "${CodePath}/repl_regs_hosp_100mradius_falsification.do"

	// robustness: check standard errors are robust to different segments
	 do "${CodePath}/repl_regs_hosp_100mradius_diffsegs.do"

	// outcome: hospital closed
	do "${CodePath}/repl_regs_hosp_100m_hospclosure.do"

	// extrapolation exercise
	do "${CodePath}/repl_extrapolation.do"
